package com.dhms.vostok1.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.dhms.vostok1.data.DEFAULT_PAGE_OFFSET
import com.dhms.vostok1.data.StatisticsType
import com.dhms.vostok1.data.ToDoStatisticsItem
import com.dhms.vostok1.data.todo.ToDoCenterData
import com.dhms.vostok1.data.todo.ToDoCenterListQueryData
import com.dhms.vostok1.data.todo.ToDoCenterState
import com.dhms.vostok1.repository.ToDoCenterListPagingSource
import com.dhms.vostok1.service.net.NetworkApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.launch

class HomeViewModel : ViewModel() {

    private val _toDoQueryConditions = MutableLiveData(
        ToDoCenterListQueryData(
            perPage = 5,
            page = DEFAULT_PAGE_OFFSET,
            matterStatus = ToDoCenterState.PENDING.value
        )
    )


    private val _latestDangersCount = MutableLiveData<Int?>()
    val latestDangersCount: LiveData<Int?> = _latestDangersCount

    private val _latestFaultsCount = MutableLiveData<Int?>()
    val latestFaultsCount: LiveData<Int?> = _latestFaultsCount

    private val _latestOverhaulsCount = MutableLiveData<Int?>()
    val latestOverhaulsCount: LiveData<Int?> = _latestOverhaulsCount

    private val _latestSpotPatrolsCount = MutableLiveData<Int?>()
    val latestSpotPatrolsCount: LiveData<Int?> = _latestSpotPatrolsCount

    private val _latestLubricationCount = MutableLiveData<Int?>()
    val latestLubricationCount: LiveData<Int?> = _latestLubricationCount

    private val _latestMaintenanceCount = MutableLiveData<Int?>()
    val latestMaintenanceCount: LiveData<Int?> = _latestMaintenanceCount

    private val _toDoStatistics = MutableLiveData<List<ToDoStatisticsItem>>(
        listOf(
            ToDoStatisticsItem(StatisticsType.LINE_PROGRESS),
            ToDoStatisticsItem(StatisticsType.CIRCLE_PROGRESS),
        )
    )
    val toDoStatistics: LiveData<List<ToDoStatisticsItem>> = _toDoStatistics


    fun getTodoDangersAndFaultsCount() {
        viewModelScope.launch {
            NetworkApi.getDangersAndFaultsCount().getOrNull()?.body()?.data?.let {
                _latestDangersCount.value = it.perilCount
                _latestFaultsCount.value = it.faultCount
            }
        }
    }

    fun getTodoOverhaulsCount() {
        viewModelScope.launch {
            val queryMap = DEFAULT_QUERY_MAP.toMutableMap()
            queryMap["agent"] = "app"
            queryMap["status"] = "[4]"
            NetworkApi.getRepairTaskList(queryMap).getOrNull()
                ?.body()?.data?.total?.let {
                    _latestOverhaulsCount.value = it
                }
        }
    }

    fun getTodoSpotPatrolsCount() {
        viewModelScope.launch {
            NetworkApi.getTaskList(
                mapOf(
                    "status" to listOf(1, 4).toString(),
                    "offset" to 1.toString(),
                    "limit" to 1.toString(),
                    "query_task_type" to 2.toString()
                )
            ).getOrNull()
                ?.body()?.data?.total?.let {
                    _latestSpotPatrolsCount.value = it
                }
        }
    }

    fun getTodoLubricationCount() {
        viewModelScope.launch {
            NetworkApi.getLubricationTaskItems(DEFAULT_QUERY_MAP).getOrNull()
                ?.body()?.data?.total?.let {
                    _latestLubricationCount.value = it
                }
        }
    }

    fun getTodoMaintenanceCount() {
        viewModelScope.launch {
            NetworkApi.getMaintenanceTaskItems(DEFAULT_QUERY_MAP).getOrNull()
                ?.body()?.data?.total?.let {
                    _latestMaintenanceCount.value = it
                }
        }
    }

    fun getToDoStatistics() {
        viewModelScope.launch {
            NetworkApi.getToDoStatistics().getOrNull()?.body()?.data?.let {
                _toDoStatistics.value = listOf(
                    ToDoStatisticsItem(StatisticsType.LINE_PROGRESS, it),
                    ToDoStatisticsItem(StatisticsType.CIRCLE_PROGRESS, it),
                )
            }
        }
    }

    fun getTodoCenterList(): Flow<PagingData<ToDoCenterData>> {
        _toDoQueryConditions.value?.let {
            return Pager(
                PagingConfig(pageSize = 5),
                initialKey = DEFAULT_PAGE_OFFSET
            ) {
                ToDoCenterListPagingSource(
                    NetworkApi,
                    _toDoQueryConditions.value!!,
                    isLoadMore = false
                )
            }.flow
        }
        return object : Flow<PagingData<ToDoCenterData>> {
            override suspend fun collect(collector: FlowCollector<PagingData<ToDoCenterData>>) {
            }
        }

    }


    companion object {

        private val DEFAULT_QUERY_MAP = mapOf(
            "status" to listOf(1, 4).toString(),
            "page" to 1.toString(),
            "per_page" to 1.toString(),
        )
    }
}